Skip to content

Enhancement/issue 8427 #8811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed

Enhancement/issue 8427 #8811

wants to merge 16 commits into from

Conversation

elsayed2440
Copy link

Describe your change:

Impelementation of 2 algorithms from the ones mentioned in the issue #8427 , namely the sliding window algorithm and Kadane's algorithm.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@elsayed2440 elsayed2440 requested a review from cclauss as a code owner June 7, 2023 23:18
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Jun 7, 2023
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty file, should be deleted

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I have modified and filled it in

>>> print(next(sw_gen))
[1, 2, 3]
>>> print(next(sw_gen))
[2, 3, 4]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test to check whether the generator stops at the correct index when it reaches the end of the list?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This algorithm doesn't fit in web_programming. I think it'd fit better under other instead

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the clarification, I have moved the files to other folder

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we already have an implementation of max subarray sum in maths/kadanes.py, other/maximum_subarray.py, maths/largest_subarray_sum.py, other/maximum_subsequence.py, and dynamic_programming/max_sum_contiguous_subsequence.py. Furthermore, dynamic_programming/max_sub_array.py and divide_and_conquer/max_subarray_sum.py both implement this algorithm has a subroutine.

... I have no idea why there are so many near-duplicate implementations of the same algorithm, but this really shouldn't be the case. I'll open an issue to address this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #8812

@tianyizheng02
Copy link
Contributor

Also, some of these commits don't pertain to the algorithm contribution itself (Create/Delete TestShiva, Add black version/delete black file, etc.), so this PR could use a rebase to get those commits deleted/squashed before it's merged

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Jun 8, 2023
@cclauss
Copy link
Member

cclauss commented Jun 8, 2023

In the commit body, the box is checked next to

  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.

yet there are three algorithm files in this pull request.

How is the first file better than

def max_sub_array_sum(a: list, size: int = 0):

Is it more concise or faster? If so, do we have a benchmark to prove that?

Example:
>>> arr = [-2, -3, 4, -1, -2, 5, -3]
>>> max_sub_array_sum(arr, len(arr))
6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a blank line to the tests pass.

Suggested change
6
6

As discussed in CONTRIBUTING.md please test your code by running on your local machine:
% python3 -m doctest -v dynamic_programming/max_sub_array_sum.py

Comment on lines +27 to +28
window = elements[i : i + window_size]
windows.append(window)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
window = elements[i : i + window_size]
windows.append(window)
windows.append(elements[i : i + window_size])

Comment on lines +20 to +22
>>> print(next(sw_gen))
[1, 2, 3]
>>> print(next(sw_gen))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>> print(next(sw_gen))
[1, 2, 3]
>>> print(next(sw_gen))
>>> next(sw_gen)
[1, 2, 3]
>>> next(sw_gen)

@cclauss cclauss closed this Jun 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants